using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;

// TODO: replace these with the processor input and output types.
using TInput = SpriterContentPipelineExtension.SpriterData;
using TOutput = SpriterContentPipelineExtension.SpriterData;
using System.IO;



namespace SpriterContentPipelineExtension
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content Pipeline
    /// to apply custom processing to content data, converting an object of
    /// type TInput to TOutput. The input and output types may be the same if
    /// the processor wishes to alter data without changing its type.
    ///
    /// This should be part of a Content Pipeline Extension Library project.
    ///
    /// TODO: change the ContentProcessor attribute to specify the correct
    /// display name for this processor.
    /// </summary>
    [ContentProcessor(DisplayName = "Spriter Content Processor")]
    public class SpriterContentProcessor : ContentProcessor<TInput, TOutput>
    {
        public override TOutput Process(TInput input, ContentProcessorContext context)
        {
            BuildSpriteSheet(input, input, context);
         
            return input;
            // TODO: process the input object, and return the modified data.
            //throw new NotImplementedException();
        }
        /// <summary>
        /// Convert sprites into sprite sheet object
        /// (Basically from XNA SpriteSheetSample project)
        /// </summary>
        public void BuildSpriteSheet(SpriterData input, SpriterData sprite, ContentProcessorContext context)
        {
            List<BitmapContent> sourceSprites = new List<BitmapContent>();
            
            // Load each sprite texture
            foreach (var folder in input.folders)
            {
              
                List<Rectangle> tmprect = new List<Rectangle>();
                foreach (var file in folder.file)
                {
                    string spriteName =Path.GetFileNameWithoutExtension(file.FileName);
                    ExternalReference<TextureContent> textureReference = new ExternalReference<TextureContent>(Path.GetDirectoryName(input.pathtofile) + @"\"+file.FileName);
                    TextureContent texture = context.BuildAndLoadAsset<TextureContent, TextureContent>(textureReference, "TextureProcessor");
                    sourceSprites.Add(texture.Faces[0][0]);
                    tmprect.Add(Rectangle.Empty);
                      
                }
                sprite.ImageRectangles.Add(tmprect);
            }

            // Pack all the sprites onto a single texture.
            BitmapContent packedSprites = SpritePacker.PackSprites(sourceSprites, sprite.ImageRectangles, context);
            sprite.Texture.Mipmaps.Add(packedSprites);

        }

    }

}